package com.xinhua.erp.service;


import com.xinhua.erp.pojo.ProductClass;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.repo.ProductClassRepo;
import com.xinhua.erp.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional

public class ProductClassService {

    @Autowired
    private ProductClassRepo productClassRepo;
    @Autowired
    private IdWorker idWorker;


    public Result findById(long id){

        return Result.OK(productClassRepo.getOne(id));

    }

    public Result findAll(){

        List<ProductClass> list = productClassRepo.findAll();

        return Result.OK(list);
    }

    public Result save(ProductClass productClass){

        productClass.setId(idWorker.nextId());

        LocalDateTime now = LocalDateTime.now();

//        productClass.setCreateTime(now);

        productClass.setCreateTime(new Date());
        productClassRepo.save(productClass);

        return Result.OK();
    }

    public Result deleteById(long id){

        productClassRepo.deleteById(id);

        return Result.OK();
    }

    public Result update(ProductClass productClass,long id){

        productClass.setId(id);

        productClassRepo.save(productClass);

        return Result.OK();

    }


    public Result findByParentId(long parentId){

        return Result.OK(productClassRepo.findByParentId(parentId));

    }

}
